package com.gxa.modules.service.impl;

import com.gxa.common.constants.Constants;
import com.gxa.common.vo.JsonResult;
import com.gxa.common.vo.R;
import com.gxa.modules.mapper.AgentMapper;
import com.gxa.modules.pojo.Agent;
import com.gxa.modules.service.AgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AgentServiceimpl implements AgentService {
    @Autowired
    private AgentMapper agentMapper;
    JsonResult jsonResult = new JsonResult();
    R r = new R();

    /**
     * 分页查询承运商
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public JsonResult findAgent(Integer currentPage, Integer pageSize) {
        int count = agentMapper.findcoubtAgent(Constants.STATUS_UNDEL);
        Integer start = (currentPage-1)*pageSize;
        Integer off = pageSize;
        List<Map> result = agentMapper.findAgent(start, off, Constants.STATUS_UNDEL);
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");
        jsonResult.setCount(count);
        jsonResult.setData(result);
        return jsonResult;
    }

    /**
     * 增加承运商
     * @param agent
     * @return
     */
    @Override
    public R addAgent(Agent agent) {
        int result = agentMapper.addAgent(agent);
        if (result>0){
            r.setCode(200);
            r.setMsg("添加成功");
        }
        return r;
    }

    /**
     * 删除承运商
     * @param agent_id
     * @return
     */
    @Override
    public R deleteAgent(Integer agent_id) {
        int i = agentMapper.deleteAgent(agent_id, Constants.STATUS_DEL);
        if (i>0){
            r.setCode(200);
            r.setMsg("删除成功");
        }
        return r;
    }

    /**
     * 批量删除承运商
     * @param agent_ids
     * @return
     */
    @Override
    public R deleteAgentList(int[] agent_ids) {
        if (agent_ids.length>0){
            int result = agentMapper.deleteAgentList(agent_ids, Constants.STATUS_DEL);
        }
        r.setCode(200);
        r.setMsg("批量删除成功");
        return r;

    }

    /**
     * 修改承运商
     * @param agent
     * @return
     */
    @Override
    public R updateAgent(Agent agent) {
        int i = agentMapper.updateAgent(agent);
        if (i>0){
            r.setCode(200);
            r.setMsg("修改成功");
        }
        return r;
    }

    /**
     * 查看承运商
     * @param agent_id
     * @return
     */
    @Override
    public JsonResult seeAgent(Integer agent_id) {
        List<Agent> agents = agentMapper.seeAgent(agent_id, Constants.STATUS_UNDEL);
        System.out.println("承运商");
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");
        jsonResult.setData(agents);
        return jsonResult;
    }

    /**
     * 条件搜索承运商
     * @param name
     * @param type
     * @param create_date
     * @return
     */
    @Override
    public JsonResult searchAgent(String name, String type, String create_date) {
        List<Agent> maps = agentMapper.searchAgent(name, type, create_date, Constants.STATUS_UNDEL);
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");
        jsonResult.setData(maps);
        return jsonResult;
    }


}
